#include "header.h"
#include "func.hpp"
int main(int argc, char *argv[])
{
    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);
    // 格式
    if (argc != 2)
        std::cout << "格式错误" << std::endl;
    // 创建套接字
    int listensockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listensockfd < 0)
        std::cout << "socket fail" << std::endl;
    // 绑定
    sockaddr_in serve;
    memset(&serve, 0, sizeof(serve));
    serve.sin_family = AF_INET;
    serve.sin_addr.s_addr = inet_addr("0.0.0.0");
    serve.sin_port = htons(atoi(argv[1]));
    int bret = bind(listensockfd, (sockaddr *)(&serve), sizeof(serve));
    if (bret < 0)
        std::cout << "bind fail" << std::endl;
    // 监听
    int lret = listen(listensockfd, LISTEN_QUEUE);
    if (lret < 0)
        std::cout << "listen fail" << std::endl;
    while (true)
    {
        // 链接
        sockaddr_in ser;
        socklen_t len;
        int servefd = accept(listensockfd, (sockaddr *)(&ser), &len);
        if (servefd < 0)
            std::cout << "accept fail" << std::endl;
        pthread_t tid;
        pthread_create(&tid, nullptr, start, &servefd);
    }
    return 0;
}